package servlet.zhouchao;

import javafx.scene.control.Alert;
import jdk.internal.util.xml.impl.Input;
import pojo.Goods;
import service.GoodsService;
import service.impl.GoodsServiceImpl;
import sun.font.Script;
import util.ExcelUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.sql.Timestamp;
import java.util.List;

@WebServlet(value = "/PullInServlet")
public class PullInServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String file="D:\\files\\Goods.xls";
        List<String> list = ExcelUtil.readByFilePath(file);
        GoodsService goodsService = new GoodsServiceImpl();
        int num = 0;
        for (int i = 1; i < list.size(); i++) {
            String[] split = list.get(i).split("`");
            Goods goods = new Goods();
            goods.setGoId(Integer.parseInt(split[0]));
            goods.setGoName(split[1]);
            goods.setGoSalesVolume(Integer.parseInt(split[2]));
            goods.setGoPraise(Integer.parseInt(split[3]));
            goods.setGoUptime(Timestamp.valueOf(split[4]));
            goods.setGoPictureUrl(split[5]);
            goods.setGoDescribe(split[6]);
            goods.setGoCost(Integer.parseInt(split[7]));
            goods.setGoPrice(Integer.parseInt(split[8]));
            goods.setGoStock(Integer.parseInt(split[9]));
            goods.setGoStock(Integer.parseInt(split[10]));
            goods.setClId(Integer.parseInt(split[11]));
            goods.setClName(split[12]);
            //goods.setGoSecKill(Integer.parseInt(split[13]));
            int j=goodsService.addGoods(goods);
            if (j > 0) {
                num++;
            }
        }
        if (num > 0) {

            response.getWriter().print(
                  "<script>alert('导入成功')</script>");
        } else {
            response.getWriter().print(
                    "<script>alert('导入成功')</script>");
        }
        request.getRequestDispatcher("GoodsPageBeanListServlet").forward(request,response);
    }
}
